﻿//-----------------------------------------------------------------------
// <copyright>
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//----------------------------------------------------------------------- 
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Xml;

namespace Fulcrum.WeakEventManager
{
    internal class WeakEventManagerGeneratorXmlConfiguration : Fulcrum.WeakEventManager.IWeakEventManagerGeneratorConfiguration
    {
        List<IWeakEventManagerConfiguration> weakEventManagers;

        public WeakEventManagerGeneratorXmlConfiguration(string configurationFile)
        {
            this.weakEventManagers = new List<IWeakEventManagerConfiguration>();
            LoadConfigurationFile(configurationFile);
        }

        public string Company
        {
            get;
            private set;
        }

        public string Namespace
        {
            get;
            private set;
        }

        public string Copyright
        {
            get;
            private set;
        }

        public ICollection<IWeakEventManagerConfiguration> WeakEventManagers
        {
            get
            {
                return this.weakEventManagers;
            }
        }

        void LoadConfigurationFile(string file)
        {
            FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);

            using (fileStream)
            {
                XmlReaderSettings readerSettings = new XmlReaderSettings();
                readerSettings.IgnoreWhitespace = true;
                readerSettings.IgnoreComments = true;

                XmlReader fileReader = XmlReader.Create(fileStream, readerSettings);

                using (fileReader)
                {
                    fileReader.MoveToContent();
                    ParseXmlSource(fileReader);
                }
            }
        }

        private void ParseXmlSource(XmlReader xmlReader)
        {
            Debug.Assert(xmlReader.IsStartElement() && 0 == String.Compare("FulcrumWeakEventManager", xmlReader.Name, StringComparison.Ordinal));

            ParseGlobalSettings(xmlReader);

            while (xmlReader.IsStartElement() && xmlReader.Name == "WeakEventManager")
            {
                switch (xmlReader.Name)
                {
                    case "WeakEventManager":
                        ProcessWeakEventManager(xmlReader);
                        break;
                    default:
                        Debug.Fail("Invalid name");
                        break;
                }
            }
        }

        private void ParseGlobalSettings(XmlReader xmlReader)
        {
            this.Company = xmlReader["Company"];
            this.Namespace = xmlReader["Namespace"];
            this.Copyright = xmlReader["Copyright"];

            xmlReader.Read();
        }

        private void ProcessWeakEventManager(XmlReader xmlReader)
        {
            var weakEventManager = new WeakEventManagerXmlConfiguration(xmlReader);

            this.weakEventManagers.Add(weakEventManager);
        }
    }
}
